package cc.toprank.syncdatatask.config;
import com.alibaba.druid.filter.config.ConfigTools;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/** 
 * description 配置mq
 * User: WeiDa
 * Date: 2017-5-7
 * Time: 12:00
 */
@Configuration
public class RabbitConfig {
	@Value("${mq.host}")
	private String host;
	@Value("${mq.username}")
	private String username;
	@Value("${mq.password}")
	private String password;
	@Value("${mq.publicKey}")
	private String publicKey;

	 	@Bean
	    public ConnectionFactory connectionFactory() {

			String pwd = null;
			try {
				pwd = ConfigTools.decrypt(publicKey,password);
			} catch (Exception e) {
				e.printStackTrace();
			}
	 		CachingConnectionFactory connectionFactory = new CachingConnectionFactory( host);
	 		connectionFactory.setUsername(username);
	 		connectionFactory.setPassword(pwd);
	 		connectionFactory.setPort(5672);
	 		return connectionFactory;
	    }

	    @Bean
	    public AmqpAdmin amqpAdmin() {
	        return new RabbitAdmin(connectionFactory());
	    }

	    @Bean
	    public RabbitTemplate rabbitTemplate() {
	        return new RabbitTemplate(connectionFactory());
	    }

	   
}
